import {
  Column,
  Entity,
  Index,
  JoinColumn,
  ManyToOne,
  OneToMany,
  OneToOne,
  PrimaryGeneratedColumn,
} from "typeorm";
import { Roles } from "./Roles";
import { UserProfiles } from "./UserProfiles";
import { UserRoles } from "./UserRoles";
import { Organizations } from "./Organizations";
import { Systems } from "./Systems";

@Index("fk_user_organization_id", ["organizationId"], {})
@Index("fk_user_system_id", ["systemId"], {})
@Index("IDX_951b8f1dfc94ac1d0301a14b7e", ["uuid"], { unique: true })
@Index("IDX_fe0bb3f6520ee0469504521e71", ["username"], { unique: true })
@Index("idx_username", ["username"], {})
@Index("idx_uuid", ["uuid"], { unique: true })
@Index("username", ["username"], { unique: true })
@Index("uuid", ["uuid"], { unique: true })
@Entity("users", { schema: "nest-demo" })
export class Users {
  @PrimaryGeneratedColumn({
    type: "bigint",
    name: "id",
    comment: "使用BIGINT应对海量用户,主键，自增ID，唯一标识",
    unsigned: true,
  })
  id: string;

  @Column("char", {
    name: "uuid",
    unique: true,
    comment: "对外暴露的UUID，避免爬虫和枚举",
    length: 36,
  })
  uuid: string;

  @Column("varchar", {
    name: "username",
    unique: true,
    comment: "用户登录名；\r\n后期 ： 允许为NULL，允许用户仅用邮箱登录",
    length: 50,
  })
  username: string;

  @Column("varchar", {
    name: "email",
    nullable: true,
    comment: "邮箱，唯一，可用于登录或找回密码",
    length: 100,
  })
  email: string | null;

  @Column("timestamp", {
    name: "email_verified_at",
    nullable: true,
    comment:
      "邮箱验证时间，NULL表示未验证；\r\n用于实现邮箱验证功能，是许多系统的基础要求；",
  })
  emailVerifiedAt: Date | null;

  @Column("varchar", {
    name: "phone",
    nullable: true,
    comment: "手机号",
    length: 20,
  })
  phone: string | null;

  @Column("varchar", {
    name: "password_hash",
    comment:
      "加密后的密码哈希值；\r\n应该存储通过 bcrypt、Argon2 等专业算法加密后的哈希值。长度 255 是为了兼容所有哈希算法",
    length: 255,
  })
  passwordHash: string;

  @Column("tinyint", {
    name: "del_flag",
    comment: " 管理用户删除状态，（0 存在，1 删除）",
    width: 1,
    default: () => "'0'",
  })
  delFlag: boolean;

  @Column("tinyint", {
    name: "status",
    comment: " 管理用户账户状态，非常必要（例如：激活、禁用、软删除标记）",
    width: 1,
    default: () => "'1'",
  })
  status: boolean;

  @Column("varchar", {
    name: "remember_token",
    nullable: true,
    comment: "“记住我”功能的令牌",
    length: 100,
  })
  rememberToken: string | null;

  @Column("int", {
    name: "system_id",
    nullable: true,
    comment: "所属系统ID",
    unsigned: true,
  })
  systemId: number | null;

  @Column("int", {
    name: "organization_id",
    nullable: true,
    comment: "所属组织ID",
    unsigned: true,
  })
  organizationId: number | null;

  @Column("timestamp", {
    name: "created_at",
    nullable: true,
    comment: "创建时间\r\n",
    default: () => "CURRENT_TIMESTAMP",
  })
  createdAt: Date | null;

  @Column("timestamp", {
    name: "updated_at",
    nullable: true,
    comment: "更新时间",
    default: () => "CURRENT_TIMESTAMP",
  })
  updatedAt: Date | null;

  @Column("timestamp", {
    name: "last_login_time",
    nullable: true,
    comment: "最后登录时间",
  })
  lastLoginTime: Date | null;

  @Column("varchar", {
    name: "last_login_ip",
    nullable: true,
    comment: "最后登录ip",
    length: 128,
  })
  lastLoginIp: string | null;

  @OneToMany(() => Roles, (roles) => roles.createBy2)
  roles: Roles[];

  @OneToMany(() => Roles, (roles) => roles.updateBy2)
  roles2: Roles[];

  @OneToOne(() => UserProfiles, (userProfiles) => userProfiles.user)
  userProfiles: UserProfiles;

  @OneToMany(() => UserRoles, (userRoles) => userRoles.user)
  userRoles: UserRoles[];

  @ManyToOne(() => Organizations, (organizations) => organizations.users, {
    onDelete: "CASCADE",
    onUpdate: "CASCADE",
  })
  @JoinColumn([{ name: "organization_id", referencedColumnName: "id" }])
  organization: Organizations;

  @ManyToOne(() => Systems, (systems) => systems.users, {
    onDelete: "CASCADE",
    onUpdate: "CASCADE",
  })
  @JoinColumn([{ name: "system_id", referencedColumnName: "id" }])
  system: Systems;
}
